home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Programming / MiniGL / demos / gravity.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-07  |  4.6 KB  |  268 lines

  1. #include <mgl/gl.h>
  2. #include <stdlib.h>
  3.  
  4. GLint width=640; GLint height=480;
  5.  
  6. GLfloat eye[3] = {15.0, 8.0, 0.0};
  7. GLfloat look[3] = {0.0, 0.0, 0.0};
  8.  
  9. GLubyte texture[768];
  10.  
  11. char *texture_ascii[] =
  12. {
  13.        "################",
  14.        "################",
  15.        "#######@@#######",
  16.        "#####@@88@@#####",
  17.        "####@8oooo8@####",
  18.        "####@o+--+o@####",
  19.        "###@8o-..-o8@###",
  20.        "###@8+-  -+8@###",
  21.        "####@o+--+o@####",
  22.        "####@8oooo8@####",
  23.        "#####@@88@@#####",
  24.        "#######@@#######",
  25.        "################",
  26.        "################",
  27.        "################",
  28.        "################"
  29. };
  30.  
  31. #define MAX_PARTICLES 512
  32.  
  33. struct Particle
  34. {
  35.     GLboolean used;     // flag: is this entry used
  36.     GLfloat x, y, z;    // Position
  37.     GLfloat dx, dy, dz; // Directional velocity
  38.     GLfloat r,g,b;      // Color
  39.     GLint age;          // Age
  40. };
  41.  
  42. struct Particle psystem[MAX_PARTICLES];
  43.  
  44. void CreateTexture()
  45. {
  46.     int i, j;
  47.     GLubyte *t = texture;
  48.  
  49.     for (i = 0; i < 16; i++)
  50.     {
  51.         for (j = 0; j < 16; j++)
  52.         {
  53.             switch (texture_ascii[i][j])
  54.             {
  55.                 case '#':
  56.                     *t++ = 0;       // R
  57.                     *t++ = 0;       // G
  58.                     *t++ = 0;       // B
  59.                     break;
  60.                 case '@':
  61.                     *t++ = 32;
  62.                     *t++ = 32;
  63.                     *t++ = 32;
  64.                     break;
  65.                 case '8':
  66.                     *t++ = 64;
  67.                     *t++ = 64;
  68.                     *t++ = 64;
  69.                     break;
  70.                 case 'o':
  71.                     *t++ = 96;
  72.                     *t++ = 96;
  73.                     *t++ = 96;
  74.                     break;
  75.                 case '+':
  76.                     *t++ = 128;
  77.                     *t++ = 128;
  78.                     *t++ = 128;
  79.                     break;
  80.                 case '-':
  81.                     *t++ = 160;
  82.                     *t++ = 160;
  83.                     *t++ = 160;
  84.                     break;
  85.                 case '.':
  86.                     *t++ = 192;
  87.                     *t++ = 192;
  88.                     *t++ = 192;
  89.                     break;
  90.                 case ' ':
  91.                     *t++ = 240;
  92.                     *t++ = 240;
  93.                     *t++ = 240;
  94.                     break;
  95.             }
  96.         }
  97.     }
  98.  
  99.     glBindTexture(GL_TEXTURE_2D, 1);
  100.     glTexImage2D(GL_TEXTURE_2D, 0, 3, 16, 16,
  101.         0, GL_RGB, GL_UNSIGNED_BYTE, texture);
  102.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  103.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  104. }
  105.  
  106.  
  107. void reshape(int w, int h)
  108. {
  109.     glMatrixMode(GL_PROJECTION);
  110.     glLoadIdentity();
  111.     gluPerspective(70.0, 1.3333333, 1.0, 100.0);
  112.  
  113.     glMatrixMode(GL_MODELVIEW);
  114.     glViewport(0, 0, w, h);
  115.     glClearColor(0.0f, 0.f, 0.f, 1.f);
  116.     glClearDepth(1.0);
  117. }
  118.  
  119. void UpdateParticles(void)
  120. {
  121.     int i;
  122.     int cRate = 5;
  123.  
  124.     for (i = 0; i < MAX_PARTICLES; i++)
  125.     {
  126.         if (psystem[i].used)
  127.         {
  128.             psystem[i].x += psystem[i].dx;
  129.             psystem[i].y += psystem[i].dy;
  130.             psystem[i].z += psystem[i].dz;
  131.  
  132.             glPushMatrix();
  133.             glTranslatef(psystem[i].x, psystem[i].y, psystem[i].z);
  134.  
  135.             glColor3f(psystem[i].r, psystem[i].g, psystem[i].b);
  136.  
  137.             glBegin(GL_QUADS);
  138.                 glTexCoord2f(0.0, 0.0);
  139.                 glVertex3f(-0.5, 0.5, 0.0);
  140.                 glTexCoord2f(1.0, 0.0);
  141.                 glVertex3f(0.5, 0.5, 0.0);
  142.                 glTexCoord2f(1.0, 1.0);
  143.                 glVertex3f(0.5, -0.5, 0.0);
  144.                 glTexCoord2f(0.0, 1.0);
  145.                 glVertex3f(-0.5, -0.5, 0.0);
  146.             glEnd();
  147.  
  148.             glPopMatrix();
  149.  
  150.             psystem[i].age++;
  151.  
  152.             if (psystem[i].age > 100)
  153.                 psystem[i].used = GL_FALSE;
  154.         }
  155.         else
  156.         {
  157.             cRate--;
  158.             if (cRate == 0) break;
  159.  
  160.             psystem[i].x = (GLfloat)(rand()%20) - 10.0;
  161.             psystem[i].y = 10.0;
  162.             psystem[i].z = (GLfloat)(rand()%20) - 10.0;
  163.  
  164.             psystem[i].dx = 0.0;
  165.             psystem[i].dy = -0.2;
  166.             psystem[i].dz = 0.0;
  167.  
  168.             psystem[i].r = (GLfloat)(rand()%255)/255.0;
  169.             psystem[i].g = (GLfloat)(rand()%255)/255.0;
  170.             psystem[i].b = (GLfloat)(rand()%255)/255.0;
  171.  
  172.             psystem[i].used = GL_TRUE;
  173.             psystem[i].age = 0;
  174.         }
  175.     }
  176. }
  177.  
  178. void DoFrame(void)
  179. {
  180.     mglLockDisplay();
  181.  
  182.     glClearColor(0.0f, 0.f, 0.f, 1.f);
  183.     glClearDepth(1.0);
  184.     glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  185.     glEnable(GL_DEPTH_TEST);
  186.     glDisable(GL_CULL_FACE);
  187.  
  188.     glMatrixMode(GL_MODELVIEW);
  189.     glLoadIdentity();
  190.     gluLookAt(eye[0], eye[1], eye[2], look[0], look[1], look[2], 0.0, 1.0, 0.0);
  191.  
  192.     glDisable(GL_TEXTURE_2D);
  193.     glDisable(GL_BLEND);
  194.  
  195.     glColor3f(0.2, 0.8, 0.2);
  196.  
  197.     glBegin(GL_QUADS);
  198.         glVertex3f(-10.0, 0.0, -10.0);
  199.         glVertex3f(10.0, 0.0, -10.0);
  200.         glVertex3f(10.0, 0.0, 10.0);
  201.         glVertex3f(-10.0, 0.0, 10.0);
  202.     glEnd();
  203.  
  204.     glEnable(GL_TEXTURE_2D);
  205.     glEnable(GL_BLEND);
  206.  
  207.     UpdateParticles();
  208.  
  209.     glFlush();
  210.  
  211.     mglUnlockDisplay();
  212.     mglSwitchDisplay();
  213. }
  214.  
  215.  
  216. void keys(unsigned char c)
  217. {
  218.  
  219.     switch (c)
  220.     {
  221.         case 0x1b:
  222.             mglExit();
  223.             break;
  224.     }
  225. }
  226.  
  227. void main(int argc, char *argv[])
  228. {
  229.     int i;
  230.  
  231.     for (i=1; i<argc; i++)
  232.     {
  233.         if (0 == stricmp(argv[i], "-width"))
  234.         {
  235.             i++;
  236.             width = atoi(argv[i]);
  237.         }
  238.         if (0 == stricmp(argv[i], "-height"))
  239.         {
  240.             i++;
  241.             height = atoi(argv[i]);
  242.         }
  243.         if (0 == stricmp(argv[i], "-window"))
  244.         {
  245.             mglChooseWindowMode(GL_TRUE);
  246.         }
  247.     }
  248.  
  249.     MGLInit();
  250.  
  251.     mglChooseVertexBufferSize(1000);
  252.     mglChooseNumberOfBuffers(3);
  253.     mglCreateContext(0,0,width,height);
  254.     mglEnableSync(GL_FALSE);
  255.  
  256.     reshape(width, height);
  257.  
  258.     CreateTexture();
  259.  
  260.     mglIdleFunc(DoFrame);
  261.     mglKeyFunc(keys);
  262.     mglMainLoop();
  263.  
  264.  
  265.     mglDeleteContext();
  266.     MGLTerm();
  267. }
  268.